Thread: Passing int & float variables from main and getting values from another function

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    Passing int & float variables from main and getting values from another function

    I have the main() - which has entries like

    Code:
    main()
    {
       int ss, ret, z;
       float yy;
    
       ret = function1(&yy, &ss, &z);
    //int function1(flat *cc, int *dd, int *k) - is it correct?
    //should there be the & in front of yy and ss?
    // How about z?
    
    }
    
    int function1(flat *cc, int *dd, int *k)
    {
    
       *k = 3 + 1
    // am I correct, I need to use *k here?
    // same for *cc and *dd
    
    }
    I appreciate some quick tips to get this order right
    It would help to see a good example too
    Last edited by uws; 02-03-2015 at 01:52 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you work with simpler examples first. For example:
    Code:
    #include <stdio.h>
    
    void add(int *result, int x, int y)
    {
        *result = x + y;
    }
    
    int main(void)
    {
        int z;
        add(&z, 5, 7);
        printf("%d\n", z);
        return 0;
    }
    Observe that I defined add before defining main. The reason is that functions should be declared before they are called. Since a function definition is also a function declaration, I found it convenient to define and hence declare add before it is called in main.

    Next, in add, I want to store the result of adding x and y to what the result pointer points to. Therefore, I dereference the result pointer, i.e., I write *result.

    In main, to pass a pointer to z as an argument to add, I take the address of z, i.e., I write &z.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    @laserlight - Thank you

    That was a good start and I got to play with it too.

    In the link below - if you scroll down on the link below where there is Green colored tick on the left hand side - right next to it/just below it, there is a code:
    c - Function main received incorrect values from the subroutine - Stack Overflow

    main() has
    int sRates, sRatem, ret;
    But the pointers are used in the function call as below - why?
    int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer);
    Why not use
    long numFrames, int sRate?

    Similarly main has float *datas, *datam;
    And the function uses float **buffer - I wonder why - these seems different from the example you gave.

    Please clarify me. Thank you.
    Last edited by uws; 02-03-2015 at 11:05 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uws
    But the pointers are used in the function call as below - why?
    int readWav(const char *const fname, long *numFrames, int *sRate, float **buffer);
    Why not use
    long numFrames, int sRate?
    Look at the implementation of the function. You will see:
    Code:
    *sRate = sndInfo.samplerate;
    // Load data
    *numFrames = sf_readf_float(sndFile, *buffer, sndInfo.frames);
    If the caller did not pass pointers, then these lines would be assigning to local variables, so the variables in the caller would not be changed.

    Quote Originally Posted by uws
    Similarly main has float *datas, *datam;
    And the function uses float **buffer - I wonder why - these seems different from the example you gave.
    The parameter named buffer corresponds to the argument &datas, and likewise you can find a statement in readWav where *buffer was used so that datas in main would be changed. datam looks like a relic of the original post: it was not used and hence could have been removed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    @laserlight - Thank you

    Curious is there another way to write it with less pointers or so .. what does it look like? If so which one is considered optimal/better?

    Secondly if the main is passing an array variable, value[] , there is no need to set dimensions inside the function at all right ... as well as free the memory inside function ... once the function is executed they disappear? Inside main, array declarations, value[100], does not need to be freed right but arrays declared as pointers and then malloced memory needs to be freed ?

    If you know of some great examples of calling functions and returning etc, pls let me know.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by uws
    Curious is there another way to write it with less pointers or so .. what does it look like? If so which one is considered optimal/better?
    Generally, if you can pass the object instead of a pointer to the object, you should do so to avoid an unnecessary level of indirection. However, because the object may be expensive to copy, or because you may want to change the value of the object and have the change reflected in the caller, you may need to pass a pointer to the object. The latter is the case here, so you cannot "write it with less pointers".

    Well, actually, you can, by using global variables, but that is a Bad Thing. If it is appropriate, you might also be able to use a struct instead of separate variables, in which case you just need to pass one pointer.

    Quote Originally Posted by uws
    Secondly if the main is passing an array variable, value[] , there is no need to set dimensions inside the function at all right ... as well as free the memory inside function ... once the function is executed they disappear?
    You cannot "set dimensions" since you have a pointer to the array's first element: the array's dimensions have already been set. You cannot free the memory inside the function since the memory might not even be dynamically allocated. Since the pointer parameter is a local variable, yes, it does get destroyed when control returns to the caller.

    Quote Originally Posted by uws
    Inside main, array declarations, value[100], does not need to be freed right but arrays declared as pointers and then malloced memory needs to be freed ?
    Yes, though it is not that the array is declared as a pointer, but that the pointer points to the first element of a dynamically allocated array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. Float function prototype inside main?
    By URS in forum C Programming
    Replies: 2
    Last Post: 04-18-2011, 03:22 AM
  3. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  4. Passing float values as void* arguments
    By Niara in forum C Programming
    Replies: 9
    Last Post: 07-15-2007, 09:52 AM
  5. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 07:30 PM